/**
 * 从子节点遍历
 * @param node
 */
const reverseMap = (node) => {
  let arr = [];
  if (node.children !== undefined) {
    node.children.forEach(function (child) {
      reverseMap(child);
    });
    arr.push(node);
  }
  return arr;
};

/**
 * 一维数组转树形数组
 * @returns {*}
 * @param arr
 * @param parentId
 * @param id
 */
const arrayToTree = (arr, parentId, id) => {
  if (!Array.isArray(arr) || arr.length < 1) return null;
  const [root] = arr.filter(item => item[parentId] === null);
  const addChildren = (node, dataList) => {
    const children = dataList
      .filter(item => item[parentId] === node[id])
      .map(item => addChildren(item, dataList));
    return { ...node, children };
  };
  return addChildren(root, arr);
};

/**
 * 一维数组转树形数组(已知parentId)
 * @returns {*}
 * @param arr
 * @param parentId
 * @param id
 */
const arrayToTreeById = (arr, parentId, id, nodeId) => {
  if (!Array.isArray(arr) || arr.length < 1) return null;
  const [root] = arr.filter(item => item[parentId] === nodeId);
  const addChildren = (node, dataList) => {
    const children = dataList
      .filter(item => item[parentId] === node[id])
      .map(item => addChildren(item, dataList));
    return { ...node, children };
  };
  return addChildren(root, arr);
};

/**
 * 树形数组转一维数组
 * @param node
 * @returns {*}
 */
const treeToArray = (node) => {
  const nodeToArray = (node, arr) => {
    const { children, ...item } = node;
    arr.push(item);
    children.forEach(child => nodeToArray(child, arr));
    return arr;
  };
  return nodeToArray(node, []);
};

/**
 * 获取所有子节点(包含当前节点)
 * @param nodes
 * @param item 当前节点
 * @param id 属性id,name
 * @returns {*[]}
 */
const findChildNode = (nodes, item, id) => {
  let nodeArr = [];
  nodeArr.push(item)
  const getNodes = (trees) => {
    for (let idx in trees) {
      if (trees.hasOwnProperty(idx)) {
        nodeArr.push(trees[idx][id])  // 遍历项目满足条件后的操作
        if (trees[idx].children) {  //存在子节点就递归
          getNodes(trees[idx].children);
        }
      }
    }
  }
  getNodes(nodes)
  return nodeArr;
};

/**
 * 获取所有父节点(包含当前节点)
 * @param nodes
 * @param item 当前节点
 * @param attr 属性id,name
 * @returns {*[]}
 */
const findParentNode = (nodes, item, attr) => {
  const targetData = {};
  const loops = (data = [], parent) => {
    return data.map(({ children, [attr]: value }) => {
      const node = {
        value,
        parent,
      };
      targetData[value] = node;
      node.children = loops(children, node);
    });
  };
  const getNode = (value) => {
    let node = [];
    let currentNode = targetData[value];
    node.push(currentNode.value);
    if (currentNode.parent) {
      node = [...getNode(currentNode.parent.value), ...node];
    }
    return node;
  };
  return getNode(item);
};

/**
 * 一维数组转树形数组
 * @returns {*}
 * @param lines
 * @param id
 * @param parentId
 * @param nodeId
 */
const etlArrayToTree = (lines, nodeId, id, parentId, nodes) => {
  return lines.reduce((res, current) => {
    if (current[parentId] === nodeId) {
      let info = nodes.find(item => {
        return item.id === current[id];
      });
      current = Object.assign({}, current, info);
      current.children = etlArrayToTree(lines, current[id], id, parentId, nodes);
      return res.concat(current);
    }
    return res;
  }, []);
};

export {
  reverseMap,
  arrayToTree,
  treeToArray,
  findChildNode,
  findParentNode,
  etlArrayToTree,
  arrayToTreeById,
};